package com.example.liang.basamovie.json;

import android.util.Log;

import com.example.liang.basamovie.bean.FIndFilmResourcesBean;
import com.example.liang.basamovie.bean.FindBean;
import com.example.liang.basamovie.bean.FindFilmCasts;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现的解析类
 */
public class FindJson {
    public static List<FindBean> getList(String data){
        try {
            JSONObject object=new JSONObject(data);
            JSONArray jsonArray = object.getJSONArray("recommendsList");
            FindBean findBean;
            FIndFilmResourcesBean fIndFilmResourcesBean;
            FindFilmCasts findFilmCasts;
            List<FindFilmCasts> list;//=new ArrayList<>();
            List<FindBean> list1=new ArrayList<>();
            for (int i = 0; i <jsonArray.length() ; i++) {
                findBean=new FindBean();
                JSONObject object1 = jsonArray.getJSONObject(i);

                findBean.setPraiseCount(object1.getInt("praiseCount"));
                findBean.setRecommend(object1.getString("recommend"));
                findBean.setCommentCount(object1.optInt("commentCount"));

                JSONObject filmResources = object1.getJSONObject("filmResources");
                fIndFilmResourcesBean=new FIndFilmResourcesBean();
                fIndFilmResourcesBean.setId(filmResources.optString("id"));
                fIndFilmResourcesBean.setFilm_title(filmResources.getString("title"));
                fIndFilmResourcesBean.setFilm_years(filmResources.getString("year"));
                JSONObject images = filmResources.getJSONObject("images");
                fIndFilmResourcesBean.setImage_url(images.getString("medium"));
                JSONObject writerInfo = object1.getJSONObject("writerInfo");
                findBean.setHeadImgUrl(writerInfo.getString("headImgUrl"));
                findBean.setNickname(writerInfo.getString("nickname"));
                findBean.setId(writerInfo.optString("id"));
                if(filmResources.has("directors")){
                    JSONArray directors = filmResources.getJSONArray("directors");
                    for (int j = 0; j < directors.length(); j++) {
                        JSONObject jsonObject = directors.getJSONObject(j);
                        fIndFilmResourcesBean.setFilm_directors(jsonObject.optString("name"));
                    }
                }
                list=new ArrayList<>();
                if(filmResources.has("casts")){
                    JSONArray casts = filmResources.getJSONArray("casts");
                    for (int k = 0; k <casts.length() ; k++) {
                        findFilmCasts=new FindFilmCasts();
                        JSONObject jsonObject = casts.getJSONObject(k);
                        findFilmCasts.setName(jsonObject.getString("name"));
                        if(jsonObject.has("avatar_photos")){
                                    JSONObject avatar_photos = jsonObject.getJSONObject("avatar_photos");
                                    if(avatar_photos.has("medium")){
                                        findFilmCasts.setImage_url(avatar_photos.getString("medium"));
                                    }else {
                                        findFilmCasts.setImage_url(avatar_photos.getString("small"));
                                    }
                                }
                        list.add(findFilmCasts);
                    }
                }
                fIndFilmResourcesBean.setList(list);
                findBean.setfIndFilmResourcesBean(fIndFilmResourcesBean);
                list1.add(findBean);



            }
            Log.e("er","解析没有出错");
            return list1;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("er","解析出错");
        return new ArrayList<>();
    }
}
